package org.abc.uiCore
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;
	import org.abc.interfaces.IVo;
	import org.abc.uiCore.vo.PicVo;
	import org.abc.uiCore.vo.TraceVo;
	

	/**
	 * @describe  	AbcPic
	 * @author  	Mr.zheng
	 * @website 	http://blog.sina.com.cn/zihua2007
	 * @time		2011-9-9 17:22
	 */
	public class AbcPic extends AbcBindView
	{
		/** 用做参考的mc */
		public var rule_LEA:MovieClip;
		/** 图像替身（stand in --替身），在loading没有结束前用户看到的头像
		 * 	如果素材中没有该对象，则不做处理
		 * */
		public var standIn_LEA:MovieClip;
		
		//public static var===============
		/** 图像BitmapData数据 */
		public static var imagePool:Dictionary= new Dictionary();
		/** 防止同时loading一个素材，load素材之前查找loadingDic是否有正在loading的素材
		 * 	如果需要load的素材正在被其他pic load 则侦听load完成事件
		 *  */
		public static var loadingDic:Dictionary=new Dictionary();
		//private var =====================
		private var _data:PicVo;
		private var _loader:Loader;
		private var _bm:Bitmap;
		private var _request:URLRequest;
		
		/**
		 * rule_LEA中添加显示对象之后，rule_LEA的尺寸会改变，
		 * 无法起到标尺的作用，因此设这两个变量记录rule_LEA的初始值
		 */
		private var ruleW:int;
		private var ruleH:int;

		public function AbcPic(vo:PicVo=null)
		{
			if (vo)
			{
				_data=vo as PicVo;
			}else {
				_data = new PicVo();
			}
			_request = new URLRequest();
		}

		/* public function */
		public function get url():String
		{
			return _data.url;
		}

		public function set url(value:String):void
		{
			_data.url=value;
			update();
		}
		/**
		 * 测试用，查看imagePool
		 * @return
		 */
		public function get imagePoolDic():Dictionary {
			var dic:Dictionary = imagePool;
			return dic;
		}
		public static function hasImage(url:String):BitmapData{
			return imagePool[url];
		}
		/* override function */
		override public function init():void{
			setMouseEnabled(rule_LEA,false);
			if(standIn_LEA)setMouseEnabled(standIn_LEA,false);
		}

		public override function update(vo:IVo=null):void
		{
			super.update(vo);
			if (vo)_data = vo as PicVo;
			
			if(ruleW==0||ruleH==0){
				ruleW=rule_LEA.width;
				ruleH=rule_LEA.height;
			}
			
			if (_data.url == "") {
				if (_bm)_bm.bitmapData = null;
				if (_loader)_loader.unloadAndStop();
				return;
			}
			//如果需要导入的图片已经存在，则将预存的BitmapData放入Bitmap容器中显示
			var imageBtm:BitmapData=hasImage(url);
			if(imageBtm){
				_bm=new Bitmap(imageBtm);
				showImge(_bm);
			}else{
				_loader=new Loader();
				_request.url=url;
				_loader.load(_request);
				_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
			}
		}

		public override function readyRecycle():void
		{
			super.readyRecycle();
			if (_loader && _loader.parent) {
				_loader.parent.removeChild(_loader);
				_loader.unloadAndStop();
				_loader = null;
			}
			if (_bm) {
				if(_bm.parent)_bm.parent.removeChild(_bm);
				_bm.bitmapData = null;
				_bm = null;
			}
			rule_LEA.scaleX = rule_LEA.scaleY = 1;
			if (standIn_LEA)standIn_LEA.visible=true;
		}
		public override function dispose():void
		{
			super.dispose();
			rule_LEA=null;
			standIn_LEA = null;
			_data = null;
			_bm = null;
			_loader=null;
			_request=null;
		}

		/* private function */
		/**
		 * load image completed
		 * @param	e
		 */
		private function onComplete(e:Event):void
		{
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
			//url为key，存储image的bitmapData信息
			if (_loader.content) {
				imagePool[url] = _loader.content["bitmapData"];
				showImge(_loader);
			}else {
				abcTrace(new TraceVo("_loader.content is null."));
			}
			
		}
		/**
		 * 显示图片 
		 * @param	display	第一次读取图片为_loader，之后为装载Bitmap
		 */
		private function showImge(display:DisplayObject):void{
			//重新设置rule_LEA的宽高
			rule_LEA.width=ruleW;
			rule_LEA.height=ruleH;
			
			setPicSize(display);
			if (standIn_LEA)standIn_LEA.visible=false;
			rule_LEA.addChild(display);
		}
		/**
		 * 根据sizeModel设置图片的显示模式：
		 * 0：根据rule_LEA的尺寸显示
		 * 1：根据rule_LEA的比例居中显示
		 * 2：显示自身的大小
		 * 
		 * @param	display	第一次读取图片为_loader，之后为装载Bitmap
		 */
		private function setPicSize(display:DisplayObject):void
		{
			switch (_data.sizeModel)
			{
				case PicVo.SIZE_SAME_RULE:
					display.width=rule_LEA.width;
					display.height = rule_LEA.height;
					break;
				case PicVo.SIZE_SCALE_RULE:
					var scaleX:Number=display.width / rule_LEA.width;
					var scaleY:Number=display.height / rule_LEA.height;
					var scale:Number;
					var offset:int;
					if (scaleX > scaleY)
					{ //以宽为标准
						scale=1 / scaleX;
						display.width*=scale;
						display.height*=scale;
						offset=int((rule_LEA.height - display.height) * .5);
						display.y=offset;
					}
					else
					{ //高为标准
						scale=1 / scaleY;
						display.width*=scale;
						display.height*=scale;
						offset=int((rule_LEA.width - display.width) * .5);
						display.x=offset;
					}
					break;
				case PicVo.SIZE_NONE_RULE:
					break;
			}
		}
	}

}
